home *** CD-ROM | disk | FTP | other *** search
/ isnet Internet / Isnet Internet CD.iso / prog / hiz / 09 / 09.exe / adynware.exe / perl / lib / site / File / CounterFile.pm next >
Encoding:
Perl POD Document  |  1999-12-28  |  6.8 KB  |  301 lines

  1.  
  2. package File::CounterFile;
  3.  
  4.  
  5. =head1 NAME
  6.  
  7. File::CounterFile - Persistent counter class
  8.  
  9. =head1 SYNOPSIS
  10.  
  11.  use File::CounterFile;
  12.  $c = new File::CounterFile "COUNTER", "aa00";
  13.  
  14.  $id = $c->inc;
  15.  open(F, ">F$id");
  16.  
  17. =head1 DESCRIPTION
  18.  
  19. This module implements a persistent counter class.  Each counter is
  20. represented by a separate file in the file system.  File locking is
  21. applied, so multiple processes might try to access the same counters
  22. at the same time without risk of counter destruction.
  23.  
  24. You give the file name as the first parameter to the object
  25. constructor (C<new>).  The file is created if it does not exist.
  26.  
  27. If the file name does not start with "/" or ".", then it is
  28. interpreted as a file relative to C<$File::CounterFile::DEFAULT_DIR>.
  29. The default value for this variable is initialized from the
  30. environment variable C<TMPDIR>, or F</usr/tmp> is no environment
  31. variable is defined.  You may want to assign a different value to this
  32. variable before creating counters.
  33.  
  34. If you pass a second parameter to the constructor, that sets the
  35. initial value for a new counter.  This parameter only takes effect
  36. when the file is created (i.e. it does not exist before the call).
  37.  
  38. When you call the C<inc()> method, you increment the counter value by
  39. one. When you call C<dec()> the counter value is decrementd.  In both
  40. cases the new value is returned.  The C<dec()> method only works for
  41. numerical counters (digits only).
  42.  
  43. You can peek at the value of the counter (without incrementing it) by
  44. using the C<value()> method.
  45.  
  46. The counter can be locked and unlocked with the C<lock()> and
  47. C<unlock()> methods.  Incrementing and value retrieval is faster when
  48. the counter is locked, because we do not have to update the counter
  49. file all the time.  You can query whether the counter is locked with
  50. the C<locked()> method.
  51.  
  52. There is also an operator overloading interface to the
  53. File::CounterFile object.  This means that you might use the C<++>
  54. operator for incrementing the counter, C<--> operator for decrementing
  55. and you can interpolate counters diretly into strings.
  56.  
  57. =head1 BUGS
  58.  
  59. It uses flock(2) to lock the counter file.  This does not work on all
  60. systems.  Perhaps we should use the File::Lock module?
  61.  
  62. =head1 INSTALLATION
  63.  
  64. Copy this file to the F<File> subdirectory of your Perl 5 library
  65. directory (often F</usr/local/lib/perl5>).
  66.  
  67. =head1 COPYRIGHT
  68.  
  69. Copyright (c) 1995-1996 Gisle Aas. All rights reserved.
  70.  
  71. This library is free software; you can redistribute it and/or
  72. modify it under the same terms as Perl itself.
  73.  
  74. =head1 AUTHOR
  75.  
  76. Gisle Aas <aas@sn.no>
  77.  
  78. =cut
  79.  
  80. require 5.002;
  81. use Carp   qw(croak);
  82. use Symbol qw(gensym);
  83.  
  84. sub Version { $VERSION; }
  85. $VERSION = sprintf("%d.%02d", q$Revision: 0.9 $ =~ /(\d+)\.(\d+)/);
  86.  
  87. $MAGIC           = "#COUNTER-1.0\n";   # first line in counter files
  88. $DEFAULT_INITIAL = 0;                  # default initial counter value
  89.  
  90. $DEFAULT_DIR     = $ENV{TMPDIR} || "/usr/tmp";
  91.  
  92. %OVERLOAD = ('++'     => \&inc,
  93.          '--'     => \&dec,
  94.          '""'     => \&value,
  95.          fallback => 1,
  96. );
  97.  
  98.  
  99. sub new
  100. {
  101.     my($class, $file, $initial) = @_;
  102.     croak "No file specified\n" unless defined $file;
  103.  
  104.     $file = "$DEFAULT_DIR/$file" unless $file =~ /^[\.\/]/;
  105.     $initial = $DEFAULT_INITIAL unless defined $initial;
  106.  
  107.     my $value;
  108.     if (-e $file) {
  109.     croak "Specified file is a directory" if -d _;
  110.     open(F, $file) or croak "Can't open $file: $!";
  111.     local($/) = "\n";
  112.     my $first_line = <F>;
  113.     $value = <F>;
  114.     close(F);
  115.     croak "Bad counter magic '$first_line' in $file" unless $first_line eq $MAGIC;
  116.     chomp($value);
  117.     } else {
  118.     open(F, ">$file") or croak "Can't create $file: $!";
  119.     print F $MAGIC;
  120.     print F $initial, "\n";
  121.     close(F);
  122.     $value = $initial;
  123.     }
  124.  
  125.     bless { file    => $file,  # the filename for the counter
  126.         value   => $value, # the current value
  127.         updated => 0,      # flag indicating if value has changed
  128.       };
  129. }
  130.  
  131.  
  132. sub locked
  133. {
  134.     exists shift->{handle};
  135. }
  136.  
  137.  
  138. sub lock
  139. {
  140.     my($self) = @_;
  141.     $self->unlock if $self->locked;
  142.  
  143.     my $fh = gensym();
  144.     my $file = $self->{file};
  145.  
  146.     open($fh, "+<$file") or croak "Can't open $file: $!";
  147.     flock($fh, 2) or croak "Can't flock: $!";  # 2 = exlusive lock
  148.  
  149.     local($/) = "\n";
  150.     my $magic = <$fh>;
  151.     if ($magic ne $MAGIC) {
  152.     $self->unlock;
  153.     croak("Bad counter magic '$magic' in $file");
  154.     }
  155.     chomp($self->{value} = <$fh>);
  156.  
  157.     $self->{handle}  = $fh;
  158.     $self->{updated} = 0;
  159.     $self;
  160. }
  161.  
  162.  
  163. sub unlock
  164. {
  165.     my($self) = @_;
  166.     return unless $self->locked;
  167.  
  168.     my $fh = $self->{handle};
  169.  
  170.     if ($self->{updated}) {
  171.     seek($fh, 0, 0) or croak "Can't seek to beginning: $!";
  172.     print $fh $MAGIC;
  173.     print $fh "$self->{value}\n";
  174.     }
  175.  
  176.     close($fh) or warn "Can't close: $!";
  177.     delete $self->{handle};
  178.     $self;
  179. }
  180.  
  181.  
  182. sub inc
  183. {
  184.     my($self) = @_;
  185.  
  186.     if ($self->locked) {
  187.     $self->{value}++;
  188.     $self->{updated} = 1;
  189.     } else {
  190.     $self->lock;
  191.     $self->{value}++;
  192.     $self->{updated} = 1;
  193.     $self->unlock;
  194.     }
  195.     $self->{value}; # return value
  196. }
  197.  
  198.  
  199. sub dec
  200. {
  201.     my($self) = @_;
  202.  
  203.     if ($self->locked) {
  204.     croak "Autodecrement is not magical in perl"
  205.         unless $self->{value} =~ /^\d+$/;
  206.     $self->{value}--;
  207.     $self->{updated} = 1;
  208.     } else {
  209.     $self->lock;
  210.     croak "Autodecrement is not magical in perl"
  211.         unless $self->{value} =~ /^\d+$/;
  212.     $self->{value}--;
  213.     $self->{updated} = 1;
  214.     $self->unlock;
  215.     }
  216.     $self->{value}; # return value
  217. }
  218.  
  219.  
  220. sub value
  221. {
  222.     my($self) = @_;
  223.     my $value;
  224.     if ($self->locked) {
  225.     $value = $self->{value};
  226.     } else {
  227.     $self->lock;
  228.     $value = $self->{value};
  229.     $self->unlock;
  230.     }
  231.     $value;
  232. }
  233.  
  234.  
  235. sub DESTROY
  236. {
  237.     my $self = shift;
  238.     $self->unlock;
  239. }
  240.  
  241.  
  242. package main;
  243.  
  244. eval join('',<DATA>) || die $@ unless caller();
  245.  
  246. 1;
  247.  
  248. __END__
  249.  
  250.  
  251. $cf = "./zz-counter-$$";  # the name for out temprary counter
  252.  
  253.  
  254. $c = new File::CounterFile $cf;
  255.  
  256. $id1 = $c->inc;
  257. $id2 = $c->inc;
  258.  
  259. $c = new File::CounterFile $cf;
  260. $id3 = $c->inc;
  261. $id4 = $c->dec;
  262.  
  263. die "test failed" unless ($id1 == 1 && $id2 == 2 && $id3 == 3 && $id4 == 2);
  264. unlink $cf;
  265.  
  266.  
  267. $id1 = (new File::CounterFile $cf, "aa98")->inc;
  268. $id2 = (new File::CounterFile $cf)->inc;
  269. $id3 = (new File::CounterFile $cf)->inc;
  270.  
  271. eval {
  272.     $c = new File::CounterFile $cf; $id4 = $c->dec; $c = undef;
  273. };
  274. die "test failed (No exception to catch)" unless $@;
  275.  
  276.  
  277. die "test failed" unless ($id1 eq "aa99" && $id2 eq "ab00" && $id3 eq "ab01");
  278. unlink $cf;
  279.  
  280.  
  281. $c = new File::CounterFile $cf, "100";
  282.  
  283. $c->lock;
  284.  
  285. $c++;  # counter is now 101
  286. $c++;  # counter is now 102
  287. $c++;  # counter is now 103
  288. $c--;  # counter is now 102 again
  289.  
  290. $id1 = "$c";
  291. $id2 = ++$c;
  292.  
  293. $c = undef;  # destroy object
  294.  
  295. unlink $cf;
  296.  
  297. die "test failed" unless $id1 == 102 && $id2 == 103;
  298.  
  299.  
  300. print "Selftest for File::CounterFile $File::CounterFile::VERSION ok\n";
  301.